package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 属性分组
 *
 * @author ct_start
 * @email Chen_Tao1023@126.com
 * @date 2021-09-12 12:28:14
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

//    /product/attrgroup/{catelogId}/withattr 获取当前分类下的所有属性带上属性分组
    @GetMapping("/{catelogId}/withattr")
    public R getAllBrandAndAttr (@PathVariable("catelogId") Long catelogId){

        // 1. 查出当前分类下的所有属性分组
            //2. 查出每个属性分组的所有属性
       List<AttrGroupWithAttrsVo> vos= attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
       return  R.ok().put("data",vos);
    }

    @Autowired
    private AttrAttrgroupRelationService relationService;
//    /product/attrgroup/attr/relation 新增关联关系的功能  页面会提交一个数组
    @PostMapping("/attr/relation")
    public R addRelation (@RequestBody List <AttrGroupRelationVo> vos){

        relationService.saveBatch(vos);
        return  R.ok();

    }


//    /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
      List<AttrEntity> attrEntities= attrService.getRelationAttr(attrgroupId);

      return R.ok().put("data",attrEntities);

    }
///product/attrgroup/{attrgroupId}/noattr/relation  请求参数里面还会收集页面带来的分页参数
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params){
//    List<AttrEntity> attrEntities= attrService.getRelationAttr(attrgroupId);
       PageUtils page= attrService.getNoRelationAttr(params,attrgroupId);
    return R.ok().put("page",page);

}

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params); // map参数里面包含了分页的请求数据,我们需要通过请求路径来获取请求哪个属性的分组
        PageUtils page=attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 根据id来查询这个分类的完整路径

        Long catelogId = attrGroup.getCatelogId();
        Long [] path=  categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
